<template>
  <div class="home-container">
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
      >搜索</van-button>
    </van-nav-bar>

    <van-tabs class="channel-tabs" v-model="active" animated swipeable border>
      <van-tab
        v-for="channel in channels"
        :key="channel.id"
        :title="channel.name">
        <article-list :channel="channel" />
      </van-tab>
      <div slot="nav-right" class="placeholder">
      </div>
      <div slot="nav-right" class="hamburger-btn" @click="isShowPopup = true">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>

    <van-popup
      v-model="isShowPopup"
      closeable
      position="bottom"
      :style="{ height: '100%' }"
      close-icon-position="top-left"
    >
      <article-edit :channels="channels" :active="active"  @update-active="updateActive" />
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user'
import ArticleList from './components/article'
import ArticleEdit from './components/article-edit'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'

export default {
  name: 'HomeIndex',
  computed: {
    ...mapState(['user'])
  },
  components: {
    ArticleList,
    ArticleEdit
  },
  data () {
    return {
      active: 0,
      channels: [],
      isShowPopup: false
    }
  },
  created () {
    this.loadChannels()
  },
  methods: {
    async loadChannels () {
      try {
        if(this.user) {
          const { data } = await getUserChannels()
          this.channels = data.data.channels
        } else {
          const data = getItem('TOUTIAO_CHANNELS')

          if(data) {
            this.channels = data
          } else {
            const { data } = await getUserChannels()
            this.channels = data.data.channels
          }
        }

      } catch (err) {
        this.$toast.fail('获取频道失败')
      }
    },
    updateActive(index, flag) {
      this.isShowPopup = flag
      this.active = index
    }
  }
}
</script>

<style scoped lang="less">
.home-container {
  padding-top: 174px;
  /deep/.van-nav-bar__title {
    max-width: unset;
  }

  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      color: #fff;
      font-size: 32px;
    }
  }

  /deep/.channel-tabs {
    .van-tab {
      border-right: 1px solid #EDEFF3;
      min-width: 200px;
      height: 80px;
      .van-tab__text {
        font-size: 27px;
        color: #777777;
      }
    }
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 2;
      width: 100%;
    }
    .van-tab--active .van-tab__text{
      color: #333 !important;
    }
    .van-tabs__line {
      width:31px !important;
      height:6px;
      background:rgba(50,150,250,1);
      border-radius:3px;
      bottom: 8px;
    }
    .van-tabs__nav {
      padding: 0;
    }
  }

  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    opacity: 0.902;
    i.toutiao {
      font-size: 33px;
    }
  }

  .placeholder {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }

  .van-popup {
    padding-top: 100px;
    box-sizing: border-box;
  }

}
</style>
